/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.data.value {
	import com.googlecode.milib.base.IChangable;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.dbg.InstanceDebugServiceProxy;

	/**
	 * @author Marek Brun
	 */
	public class MLNumberValue extends MLBroadcastClass implements IChangable {

		//DATA:	Number //old value
		public function get event_Changed():String{ return 'Changed'; }
		
		private var value:Number=0;
		private static const INSTANCES_BY_ID:Object={};		private var ID:String;
		
		public function MLNumberValue(id:String=null):void {
			
			if(id){
				if(INSTANCES_BY_ID[id]){
					throw new Error('There is already MLNumberValue with id "'+id+'". Please use MLNumberValue.getInstanceForName("'+id+'") static method instead.');
				}
				INSTANCES_BY_ID[id]=this;
				ID=id;
				if(id && InstanceDebugServiceProxy.isDebuggerEnabled()){
					InstanceDebugServiceProxy.getMLDBGMain().regDynamicNumber(this);
				}
			}
		}
		
		public static function getInstanceForID(id:String):MLNumberValue {
			if(!INSTANCES_BY_ID[id]){ new MLNumberValue(id); }
			return INSTANCES_BY_ID[id];
		}
		
		public function getID():String { return ID; }
				public function gotID():Boolean { return ID!=null; }
		
		public function get v():Number {
			return value;
		}
		
		public function set v(n:Number):void {
			if(n===value) { return; }
			var oldValue:Number=value;
			value=n;
			broadcastEvent(event_Changed, oldValue);
		}
		
	}
}
